﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections;

namespace CalemShop
{
    public partial class FrmPrintBill : Form
    {
        PrintDocument doc = new PrintDocument();
        Font fontBody = new Font("Verdana", 11);
        Font fontHeader = new Font("Verdana", 11, FontStyle.Bold);
        Font fontFooter = new Font("Verdana", 9);
        public FrmPrintBill()
        {
            InitializeComponent();
            doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);

        }

        private void btPrintSetup_Click(object sender, EventArgs e)
        {
            DialogResult rs = printDialog1.ShowDialog(this);
            if (rs == DialogResult.OK)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
              doc.Print();
        }

        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            
            Graphics g = e.Graphics;
            paintView(g);
        }

        private void btPreview_Click(object sender, EventArgs e)
        {
            paintView(Graphics.FromHwnd(view.Handle));
        }

        private void paintView(Graphics g)
        {
            // Paint header :
            float lineSep = 5;
            int lineW = 270;
            float fontH = fontHeader.GetHeight(g);
            float fontB = fontBody.GetHeight(g);
            float fontF = fontFooter.GetHeight(g);
            doc.DefaultPageSettings.Margins.Left = 2;
            doc.DefaultPageSettings.Margins.Right = 2;
            RectangleF rectF1 = new RectangleF(5, 5, lineW, fontH);
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            g.DrawString("THỜI TRANG TRẺ EM", fontHeader, Brushes.Black, rectF1, format);
            float y = lineSep + fontH + 15;

            rectF1 = new RectangleF(5, y, lineW, fontH);
            g.DrawString("*** CÀ LEM ***", fontHeader, Brushes.Black, rectF1, format);

            y += fontH + lineSep;
            rectF1 = new RectangleF(5, y, lineW, 3 * fontF);
            g.DrawString("197 Phan Đình Phùng, p.Thanh Bình TP.Biên Hòa", fontFooter, Brushes.Black, rectF1, format);
            y += fontH + 3* lineSep;
            rectF1 = new RectangleF(5, y, lineW, fontF);
            g.DrawString("***************************", fontFooter, Brushes.Black, rectF1, format);
            //g.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
            // Draw Invoice row
            //------------------------------------------------------------------------------
            float x = 4;
            lineW = 275;
            y += fontH + 2* lineSep;
            string tab4 = "    ";
            string tab8 = "        ";
            string tab12 = "            ";
            RectangleF rectBody = new RectangleF(x, y, lineW, fontB);
            format.Alignment = StringAlignment.Near;
            g.DrawString("Mã S.Phẩm" + tab4 + "SL" + tab8 + "Giá" + tab12 + "T.Tiền", fontBody, Brushes.Black, rectBody, format);
            y += fontH + lineSep;
            RectangleF rectC1 = new RectangleF(x, y, 75, fontB);
            RectangleF rectC2 = new RectangleF(x + rectC1.Width + 2 , y, 30, fontB);
            RectangleF rectC3 = new RectangleF(rectC2.X + rectC2.Width + 2, y, 80, fontB);
            RectangleF rectC4 = new RectangleF(rectC3.X + rectC3.Width + 2, y, 90, fontB);
            g.DrawRectangle(Pens.Black, Rectangle.Round(rectC1));
            g.DrawRectangle(Pens.Black, Rectangle.Round(rectC2));
            g.DrawRectangle(Pens.Black, Rectangle.Round(rectC3));
            g.DrawRectangle(Pens.Black, Rectangle.Round(rectC4));

        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            doc.Print();
        }
    }
}
